package com.collect.discord.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.collect.common.redis.RedisCache;
import com.collect.common.redis.RedisKeyUtils;
import com.collect.common.utils.date.DateUtils;
import com.collect.discord.web.domain.SensitiveWord;
import com.collect.discord.web.mapper.SensitiveWordMapper;
import com.collect.discord.web.service.ISensitiveWordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author :hky
 * @ Date  : Created in 15:21 2022-09-25
 * @ Version discord
 * @ Decribtion : 敏感词
 */
@Service
public class SensitiveWordServiceImpl extends ServiceImpl<SensitiveWordMapper, SensitiveWord> implements ISensitiveWordService {
    @Resource
    private RedisCache redisCache;

    @Override
    public List<SensitiveWord> getSensitiveWordList(SensitiveWord sensitiveWord) {
        QueryWrapper<SensitiveWord> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(sensitiveWord.getWord()), "word", sensitiveWord.getWord());
        queryWrapper.eq(StringUtils.isNotBlank(sensitiveWord.getDicType()), "dic_type", sensitiveWord.getDicType());
        List<SensitiveWord> list = list(queryWrapper);
        return list;
    }

    @Override
    public boolean saveSensitiveWord(SensitiveWord sensitiveWord) {
        sensitiveWord.setCreateTime(DateUtils.getStdDate());
        boolean save = save(sensitiveWord);
        if(save){
            redisCache.setCacheMapValue(RedisKeyUtils.SENSITIVE,sensitiveWord.getWord().toLowerCase(),sensitiveWord);
            redisCache.setCacheObject("lastModified",System.currentTimeMillis());
        }
        return save;
    }

    @Override
    public boolean updateSensitiveWord(SensitiveWord sensitiveWord) {
        boolean update = updateById(sensitiveWord);
        if(update){
            redisCache.setCacheMapValue(RedisKeyUtils.SENSITIVE,sensitiveWord.getWord().toLowerCase(),sensitiveWord);
            redisCache.setCacheObject("lastModified",System.currentTimeMillis());
        }
        return update;
    }

    @Override
    public boolean delSensitiveWord(List<Long> idList) {
        QueryWrapper<SensitiveWord> wrapper = new QueryWrapper<>();
        wrapper.in("id",idList);
        List<SensitiveWord> list = list(wrapper);
        boolean delete = removeByIds(idList);
        if(delete){
            for (SensitiveWord sensitiveWord : list) {
                redisCache.delCacheMapValue(RedisKeyUtils.SENSITIVE, sensitiveWord.getWord().toLowerCase());
                redisCache.setCacheObject("lastModified",System.currentTimeMillis());
            }
        }
        return delete;
    }

    /**
     * 输出字典表的值
     * @param stop
     * @param httpServletResponse
     */
    @Override
    public void getDic(Boolean stop, HttpServletResponse httpServletResponse) throws IOException {
        List<SensitiveWord> sensitiveList = redisCache.getAllMapValue(RedisKeyUtils.SENSITIVE);
        if(stop){
            sensitiveList = sensitiveList.stream().filter(e -> e.getDicType().equals("stop")).collect(Collectors.toList());
        }else{
            sensitiveList = sensitiveList.stream().filter(e -> e.getDicType().equals("dic")).collect(Collectors.toList());

        }
        String eTag = sensitiveList.hashCode()+ "-" + sensitiveList.size();
        long lastTime = redisCache.getCacheObject("lastModified");
        httpServletResponse.setDateHeader("Last-Modified",lastTime);
        httpServletResponse.setHeader("ETag", eTag);
        httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        try (OutputStream outputStream = httpServletResponse.getOutputStream();){
            StringBuilder str = new StringBuilder();
            for (SensitiveWord sensitiveWord : sensitiveList) {
                str.append(sensitiveWord.getWord());
                str.append("\n");
            }
            outputStream.write(str.toString().getBytes("utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        httpServletResponse.flushBuffer();
    }



}
